import { Init, Provide } from '@midwayjs/decorator';
import { BaseService } from '@cool-midway/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { AnchorDynamicEntity } from '../entity/dynamic';
import { DemoUserInfoEntity } from '../../demo/entity/info';
/**
 * anchorDynamic模块-动态信息服务
 */
@Provide()
export class AnchorDynamicService extends BaseService {
  @InjectEntityModel(AnchorDynamicEntity)
  anchorDynamicEntity: Repository<AnchorDynamicEntity>;

  @InjectEntityModel(DemoUserInfoEntity)
  demoUserInfoEntity: Repository<DemoUserInfoEntity>;

  @Init()
  async init() {
    await super.init();
    this.setEntity(this.anchorDynamicEntity);
  }

  async add(param: any) {
    // 调用原本的add，如果不需要可以不用这样写，完全按照自己的新增逻辑写
    let anchorItem = await this.demoUserInfoEntity.findBy({ id: Number(param.anchorId) })
    let name = anchorItem[0].name
    let avatar = anchorItem[0].avatar
    param.anchorName = name
    param.avatar = avatar
    const result: any = await super.add(param);
    return result;
  }

  async update(param: any) {
    // 调用原本的add，如果不需要可以不用这样写，完全按照自己的新增逻辑写
    let anchorItem = await this.demoUserInfoEntity.findBy({ id: Number(param.anchorId) })
    let name = anchorItem[0].name
    let avatar = anchorItem[0].avatar
    param.anchorName = name
    param.avatar = avatar
    const result: any = await super.update(param);
    return result;
  }
}
